package cn.sycoder.controller;

import cn.sycoder.dto.ProductNameDto;
import cn.sycoder.entity.LoginRecords;
import cn.sycoder.entity.Product;
import cn.sycoder.exception.BusinessException;
import cn.sycoder.mapper.LoginRecordsMapper;
import cn.sycoder.mapper.ProductMapper;
import cn.sycoder.resp.Code;
import cn.sycoder.resp.Resp;
import cn.sycoder.resp.RespCode;
import cn.sycoder.service.ILoginRecordsService;
import cn.sycoder.service.IProductService;
import cn.sycoder.service.RedisService;
import cn.sycoder.vo.LoginRecordsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api")
@Api(tags = "数据可视化")
@Transactional(rollbackFor = Exception.class)
public class CountController {

    @Autowired
    ILoginRecordsService loginRecordsService; // 登录日志

    @Autowired
    LoginRecordsMapper loginRecordsMapper; // 登录日志

    @Autowired
    RedisService redisService;

    @Autowired
    ProductMapper productMapper;// 产品管理

    @Autowired
    private RedisTemplate<String, Object> redisTemplate; // redis

    @PostMapping("/loginUser/top5")
    @ApiOperation(value = "登录用户前五名展示")
    public Map<String, Object> loginUser() {

        Map<String, Object> response = new HashMap<>();

        List<LoginRecordsVo> list = null;
        try {
            // 自定义sql查询用户名和历史登录次数
            list = loginRecordsMapper.select();

        } catch (BusinessException e) {
            throw new BusinessException(Code.BUSINESS_ERR, "处理业务异常，请稍后再试！");
        }
        if (list != null) {
            response.put("success", true);
            response.put("data", list);
        } else {
            response.put("msg", "查询失败");
        }
        return response;
    }


    @PostMapping("/OutInventory/top5")
    @ApiOperation(value = "出库物品前五名展示")
    public Map<String, Object> outInventory() {
        Map<String, Object> response = new HashMap<>();
        List<ProductNameDto> list;
        try {
            list = productMapper.selectName();
        } catch (BusinessException e) {
            throw new BusinessException(Code.BUSINESS_ERR, "处理业务异常，请稍后再试！");
        }

        if (list!= null && list.size() > 0) {
            // 遍历前五名产品
            for (ProductNameDto productDto : list) {
                String productName = productDto.getName();
                // 先从Redis中获取该产品对应的信息（以产品名称为key，产品信息对象为value）
                Object redisProducts = redisTemplate.opsForValue().get(productName);
                if (redisProducts == null) {
                    // 如果不存在，直接将当前产品信息存入Redis
                    redisTemplate.opsForValue().set(productName, productDto);
                } else {
                    // 如果存在，更新Redis中的产品出库数量
                    if (redisProducts instanceof ProductNameDto) { // 判断
                        ProductNameDto outProducts = (ProductNameDto) redisProducts;
                        // 更新Redis中出库数量
                        outProducts.setInventoryRemove(productDto.getInventoryRemove()); // 获取最新数据
                        redisTemplate.opsForValue().set(productName, outProducts); // 存储redis
                    }
                }
            }
            response.put("data", list);
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("msg", "查询失败！");
        }
        return response;
    }


    @PostMapping("/Input/Inventory")
    @ApiOperation(value = "产品入库总数")
    public Map<String, Object> inputInventoryCount(){

        Map<String, Object> response = new HashMap<>();

        List<ProductNameDto> list;
        try {
            list = productMapper.selectInputCount();

        } catch (BusinessException e) {
            throw new BusinessException(Code.BUSINESS_ERR, "处理业务异常，请稍后再试！");
        }

        if (list != null) {
            response.put("data", list);
            response.put("success", true);

        } else {
            response.put("success", false);
            response.put("msg", "查询失败！");
        }
        return response;
    }


    @PostMapping("/Output/Inventory")
    @ApiOperation(value = "产品出库总数")
    public Map<String, Object> outputInventoryCount(){

        Map<String, Object> response = new HashMap<>();

        List<ProductNameDto> list;
        try {
            list = productMapper.selectOutputCount();

        } catch (BusinessException e) {
            throw new BusinessException(Code.BUSINESS_ERR, "处理业务异常，请稍后再试！");
        }

        if (list != null) {
            response.put("data", list);
            response.put("success", true);

        } else {
            response.put("success", false);
            response.put("msg", "查询失败！");
        }
        return response;
    }
}
